/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.db.table;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.edi.db.dialect.Dialect;
import com.edi.db.type.NullableType;
import com.edi.util.SqlTypeHelper;

/**
 * 存储字段类型
 *
 */
public class FieldEntity {
	private static final Log log = LogFactory.getLog(FieldEntity.class);
//	private String tableName;
	private String fieldName;
	private NullableType fieldType;
	private boolean isIdentify;
	private boolean nullable = true;
	private int size;		//字段容量
	private int precision;	//小数点前位数
	private int scale;		//小数点后位数
	private String defaultValue;
	public String getFieldName() {
		return fieldName;
	}
	public void setFieldName(String fieldName) {
		this.fieldName = fieldName.toLowerCase();
	}
	public NullableType getFieldType() {
		return fieldType;
	}
	public void setFieldType(NullableType fieldType) {
		this.fieldType = fieldType;
	}
	
	public void setFieldType(String fieldType){
		this.fieldType = SqlTypeHelper.getSqlType(fieldType);
	}
	
	public boolean isIdentify() {
		return isIdentify;
	}
	public void setIdentify(boolean isPrimary) {
		this.isIdentify = isPrimary;
		this.nullable = false;
	}
//	public String getTableName() {
//		return tableName;
//	}
//	public void setTableName(String tableName) {
//		this.tableName = tableName;
//	}
	public boolean isNullable() {
		return nullable;
	}
	public void setNullable(boolean nullable) {
		this.nullable = nullable;
	}
	public String getDefaultValue() {
		return defaultValue;
	}
	public void setDefaultValue(String defauleValue) {
		this.defaultValue = defauleValue;
	}
	
	/**
	 * equals方法，对外屏蔽掉，避免使用
	 * @param fieldEntity
	 * @return
	 */
	private boolean equals(FieldEntity fieldEntity){
		boolean returnVal=true;
		if(fieldEntity.getFieldName()!=null&&this.getFieldName()!=null)
			if(!fieldEntity.getFieldName().equals(this.getFieldName())){
				returnVal=false;
				return returnVal;
			}
		//如包含 text，datetime 等 不需要规定长度的类型，直接返回TRUE cya 2009-4-29
//		log.debug(" qlTypeHelper.getSpecialTypeList() ="+SqlTypeHelper.getSpecialTypeList().size());
//		log.debug("fieldEntity.getFieldType().getName() ="+fieldEntity.getFieldType().getName());
		boolean flg=SqlTypeHelper.getSpecialTypeList().contains(fieldEntity.getFieldType().getName());
//		log.debug(" flg ="+flg);
		if(flg)
			return returnVal;
//		log.debug(" fieldEntity.getSize() ="+fieldEntity.getSize());
//		log.debug(" this.getSize() ="+this.getSize());
		if(fieldEntity.getSize()!=this.getSize()){
			returnVal=false;
			return returnVal;
		}
		
		//精度,小数位 暂时不做比较  cya 2009-4-29
//		log.debug(" fieldEntity.getPrecision() ="+fieldEntity.getPrecision());
//		log.debug(" this.getPrecision() ="+this.getPrecision());
//		if(fieldEntity.getPrecision()!=this.getPrecision()){
//			returnVal=false;
//			return returnVal;
//		}
//		log.debug(" fieldEntity.getScale() ="+fieldEntity.getScale());
//		log.debug(" this.getScale() ="+this.getScale());
//		if(fieldEntity.getScale()!=this.getScale()){
//			returnVal=false;
//			return returnVal;
//		}
		
//		if(fieldEntity.getDefauleValue()!=null&&this.getDefauleValue()!=null&&!"".equals(this.getDefauleValue())&&!"".equals(fieldEntity.getDefauleValue()))
//			if(!fieldEntity.getDefauleValue().equals(this.getDefauleValue())){
//				returnVal=false;
//				return returnVal;
//			}
		return returnVal;
	}
	/**
	 * 重载equals方法，判断两个对象是否相等
	 * @param fieldEntity
	 * @return
	 */
	public boolean equals(FieldEntity fieldEntity,Dialect dialect){
		
		if (!equals(fieldEntity))
			return false;
		
		return dialect.isEqualType(this.fieldType, fieldEntity.getFieldType());
	}
	/**
	 * 获取该字段在数据库中的长度
	 * @return
	 */
	public int getSize() {
		return size;
	}
	/**
	 * 设置该字段在数据库中的长度
	 * @param colmnLen
	 */
	public void setSize(int colmnLen) {
		this.size = colmnLen;
	}
	public int getPrecision() {
		return precision;
	}
	public void setPrecision(int precision) {
		this.precision = precision;
	}
	public int getScale() {
		return scale;
	}
	public void setScale(int scale) {
		this.scale = scale;
	}
	 
	
	

}
